package GUInemeanshop;


import components.NButton;
import components.NPanel;
import components.NTable;
import components.NTablemod2;
import components.ProductCell;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import nemeanshop.NemeanShop;
import nemeanshop.Product;

/**
 *
 * @author Ragnar
 */
public class BasketPanel extends JPanel
{   
    
    private JScrollPane         scroll;
    private ArrayList<Product>  plist;
    private NTablemod2          mod2;
    private NTable              tab;
    private Dimension           dim     = new Dimension(400,400);
    private NemeanShop          ns;
    private BasketBottom        bpb;
    
    public BasketPanel(final NemeanShop ns)
    {   
        
        setLayout(new BorderLayout());
        setPreferredSize(dim);
        this.setMinimumSize(dim);
        
        this.ns     = ns;
        bpb         = new BasketBottom(this, ns);
        plist       = new ArrayList<Product>();
        mod2        = new NTablemod2(plist, this);
        tab         = new NTable(mod2, ns);
        
        tab.setDefaultRenderer(Product.class, new ProductCell(ns, mod2, tab));
        tab.setDefaultEditor(Product.class, new ProductCell(ns, mod2, tab));
        tab.setRowHeight(40);
        tab.setSelectionBackground(Color.white);
        tab.setSelectionForeground(Color.white);
        tab.setFocusable(false);
        
        scroll      = new JScrollPane(tab);
        
        
        this.add(scroll,    BorderLayout.CENTER);
        this.add(bpb,       BorderLayout.SOUTH);
       
    }
    
    public void updateOrder(){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                mod2.update(ns.getOrder().getBasket());
                bpb.updateTot("" + ns.getOrder().getTotal());
            }
        });
    }
    
    public void reset()
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                mod2.reset();
                bpb.updateTot("0");
            }
        });
    }
    
    
}

class BasketBottom extends NPanel{
    
    private BottomRight br;
    private BottomLeft  bl;
    private Dimension d     = new Dimension(1200,100);
   
    
    public BasketBottom(BasketPanel bp, NemeanShop ns){
        this.br  = new BottomRight(bp, ns);
        this.bl  = new BottomLeft(bp, ns);
        this.setPreferredSize(d);
        this.setBackground(new Color(238,233,233));
        this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        setLayout(new GridLayout(1,2));
        add(bl);
        add(br);
        
    }
    
    public void updateTot(String t){
        bl.updateTot(t);
    }
}

class BottomLeft extends NPanel{
    
    private JLabel sum;
    private JLabel totalt = new JLabel("TOTALT: ");
    
    
    public BottomLeft(BasketPanel bp, NemeanShop ns){
        
        setLayout(new GridLayout(1,3));
        sum = new JLabel("0");
        sum.setFont(new Font("Garamond", Font.BOLD , 24));
        totalt.setFont(new Font("Garamond", Font.BOLD , 24));
        totalt.setBackground(null);
        sum.setBackground(null);
        this.setBackground(null);
        add(totalt);
        add(sum);
        
        
    }
    
    public void updateTot(String t){
        sum.setText(t);
    }
}


class BottomRight extends NPanel{
    
    
    private NButton     regsalg = new NButton("Registrer salg!");
    private NemeanShop  ns;
    private BasketPanel bp;
    
    public BottomRight(final BasketPanel bp, final NemeanShop ns){
        
        this.bp = bp;
        this.ns = ns;
        setLayout(new GridLayout(1,1));
        regsalg.setFont(new Font("Garamond", Font.BOLD , 24));
        regsalg.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        regsalg.setBackground(null);
        this.setBackground(null);
        
        regsalg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                ns.registerOrder(ns.getOrder());
                bp.reset();
                ns.getOrder().reset();
                
            }
        });
        
        add(regsalg);
        
    }
}
